package com.adv.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.adv.mapper.AdvertimgMapper;
import com.adv.mapper.AdvertposMapper;
import com.adv.model.Advertimg;
import com.adv.model.AdvertimgSqlParam;
import com.adv.model.Advertpos;
import com.adv.model.AdvertposSqlParam;
import com.adv.util.Constant;
import com.adv.util.DozerUtil;
import com.adv.vo.AdverposForm;

@Component
public class AdvertiserService {

	@Autowired
	private AdvertposMapper mapper;

	@Autowired
	private AdvertimgMapper imgMapper;

	public List<AdverposForm> getList() {
		AdvertposSqlParam param = new AdvertposSqlParam();
		param.createCriteria().andTypeEqualTo(Constant.AdverposType.ADVERTISER.getCode());
		List<Advertpos> list = mapper.selectByParam(param);
		List<AdverposForm> formList = DozerUtil.mapList(list, AdverposForm.class);
		if (!CollectionUtils.isEmpty(formList)) {
			Advertpos advertpos = null;
			for (int i = formList.size() - 1; i > -1; i--) {
				advertpos = list.get(i);
				if (null != advertpos && !CollectionUtils.isEmpty(advertpos.getAdvertimgList())) {
					formList.get(i).setIcon(advertpos.getAdvertimgList().get(0).getIcon());
				}
			}
		}
		return formList;
	}

	public AdverposForm getAdverposFormById(Integer id) {
		Advertpos advertpos = mapper.selectByPrimaryKey(id);
		AdverposForm form = DozerUtil.mapObj(advertpos, AdverposForm.class);
		if (null != form && !CollectionUtils.isEmpty(advertpos.getAdvertimgList())) {
			form.setIcon(advertpos.getAdvertimgList().get(0).getIcon());
		}
		return form;
	}

	// 新建和保存消息
	public Map<String, Object> saveAdvertiser(AdverposForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form) {
			if (StringUtils.isEmpty(form.getRegistertime())) {
				form.setRegistertime(Constant.SDFCOMMON.format(new Date()));
			}
			form.setType(Constant.AdverposType.ADVERTISER.getCode());
			Advertpos record = DozerUtil.mapObj(form, Advertpos.class);
			if (null != form.getId()) {
				int num = mapper.updateByPrimaryKeySelective(record);
				if (num == 1) {
					num = insertAdvertimg(form, form.getId());
					if (num == 1) {
						resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
					}
				}
			} else {
				int num = mapper.insertSelective(record);
				if (num == 1) {
					num = insertAdvertimg(form, record.getId());
					if (num == 1) {
						resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
					}
				}
			}
		}
		return resultMap;
	}

	private int insertAdvertimg(AdverposForm form, int id) {
		AdvertimgSqlParam param = new AdvertimgSqlParam();
		param.createCriteria().andLinkidEqualTo(id);
		List<Advertimg> advertimgList = imgMapper.selectByParam(param);
		if (CollectionUtils.isEmpty(advertimgList)) {
			Advertimg advertimg = new Advertimg();
			advertimg.setLinkid(id);
			advertimg.setIcon(form.getIcon());
			advertimg.setImgtype(Constant.ImageType.ICON.getCode());
			advertimg.setBusinesstype(Constant.BusinessType.ADVERT.getCode());
			return imgMapper.insertSelective(advertimg);
		} else {
			Advertimg advertimg = advertimgList.get(0);
			advertimg.setIcon(form.getIcon());
			return imgMapper.updateByPrimaryKey(advertimg);
		}
	}

	// 修改广告商
	public Map<String, Object> editAdvertiser(AdverposForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			Advertpos record = DozerUtil.mapObj(form, Advertpos.class);
			int num = mapper.updateByPrimaryKeySelective(record);
			if (num == 1) {
				resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
			}
		}
		return resultMap;
	}

	// 删除广告商
	public Map<String, Object> deleteAdvertiser(AdverposForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			AdvertposSqlParam param = new AdvertposSqlParam();
			param.createCriteria().andParentidEqualTo(form.getId());
			List<Advertpos> list = mapper.selectByParam(param);
			if (CollectionUtils.isEmpty(list)) {
				int num = mapper.deleteByPrimaryKey(form.getId());
				if (num == 1) {
					AdvertimgSqlParam imgParam = new AdvertimgSqlParam();
					imgParam.createCriteria().andLinkidEqualTo(form.getId());
					imgMapper.deleteByParam(imgParam);
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			} else {
				resultMap.put(Constant.RESULTKEY, Constant.HASRELATEDRESOURCE);
			}
		}
		return resultMap;
	}

}